package net.ojbk.study.curriculumDesign.web.handlers.impl.usermanage;

import net.ojbk.study.curriculumDesign.beans.APIResult;
import net.ojbk.study.curriculumDesign.beans.usermanage.User;
import net.ojbk.study.curriculumDesign.common.api.APICodeConstant;
import net.ojbk.study.curriculumDesign.service.usermanage.IUserService;
import net.ojbk.study.curriculumDesign.web.handlers.usermanage.IUserController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("user")
public class UserControllerImpl implements IUserController {
    @Autowired
    @Qualifier("userService")
    /**
     * 声明变量
     */
    private IUserService userService;

    @Autowired
    private HttpSession session;

    @RequestMapping("login")
    public String index(){
        return "userback/jspp/login";
    }
    /**
     * post一个用户信息
     */
    @Override
    @RequestMapping(value="adduser",method= RequestMethod.POST)
    @ResponseBody
    public Object user(@RequestBody User user) {
        if(user.getAge()<12 || user.getAge()>100)
        return APIResult.createError(new APICodeConstant(),"添加失败");
        userService.addOneUser(user);
        return APIResult.createSuccessMessage(new APICodeConstant(),"添加成功");
    }

    /**
     * 删除一个用户信息
     */
    @Override
    @RequestMapping(value="delete/{uid}",method= RequestMethod.DELETE)
    @ResponseBody
    public Object user(@PathVariable("uid") String uid) {
    userService.deleteOneUser(new User(uid));
    return APIResult.createSuccessMessage(new APICodeConstant(),"删除成功"+uid);
        }

    /**
     * put更新用户信息
     */
    @Override
    @RequestMapping(value="updateuser",method= RequestMethod.PUT)
    @ResponseBody
    public Object user_put(@RequestBody User user) {
        if(user.getAge()<12 || user.getAge()>100)
        return APIResult.createError(new APICodeConstant(),"更新失败");
        userService.updateOneUser(user);

        return APIResult.createSuccessMessage(new APICodeConstant(),"更新成功");
    }
    /**
     * 查询全部用户信息
     * @return
     */
    @Override
    @RequestMapping(value="queryuser",method= RequestMethod.GET)
    @ResponseBody
    public Object user(){
        return userService.getAllUser();
    }

    @Override
    @RequestMapping(value = "getuser",method = RequestMethod.POST)
    @ResponseBody
    public Object getUser(@RequestBody User user) {
        User u=null;
        u=(User)session.getAttribute("user");
        if (u!=null)return APIResult.createError(new APICodeConstant(),"您已登录");
        if (userService.getOneUser(user)==null)
            return APIResult.createFail(new APICodeConstant(),"登录失败");
        else {
            User user1=userService.getOneUser(user);
            session.setAttribute("user",user1);//用于测试
            return APIResult.createSuccessMessage(new APICodeConstant(),"登录成功");
        }
    }
}
